package com.cloudcode.common.exception.handle;

import com.cloudcode.common.api.ApiResult;
import com.cloudcode.common.exception.exception.DataBaseException;
import com.cloudcode.common.exception.exception.ParamException;
import com.cloudcode.common.exception.exception.SystemException;
import com.cloudcode.common.exception.exception.TokenException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;

/**
 * @author yunst
 */
@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 服务器未知运行时异常
     *
     * @param e 异常对象
     * @return ResponseResult
     */


    /**
     * Valid校验参数的报错
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ApiResult handle(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        //对校验错误信息进行封装，并输出到日志
        BindingResult result = e.getBindingResult();
        StringBuilder errorMessage = new StringBuilder();
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            errors.forEach(p ->{
                FieldError fieldError = (FieldError) p;
                errorMessage.append(fieldError.getDefaultMessage());
            });

        }
        return ApiResult.fail().setMessage(errorMessage.toString());
    }



    @ExceptionHandler(value = SystemException.class)
    public ApiResult handle(SystemException e) {
        log.error(e.getMessage());
        return ApiResult.fail().setMessage("system error msg: " + e.getMessage());
    }

    /**
     * token
     *
     * @param e 异常对象
     * @return ResponseResult
     */
    @ExceptionHandler(value = TokenException.class)
    public ApiResult handle(TokenException e) {
        log.error(e.getMessage());
        return ApiResult.fail().setMessage("token: " + e.getMessage());
    }

    /**
     * 服务器未知运行时异常
     *
     * @param e 异常对象
     * @return ResponseResult
     */
    @ExceptionHandler(value = Exception.class)
    public ApiResult handle(Exception e) {
        log.error(e.getMessage());
        return ApiResult.fail().setMessage("error msg: " + e.getMessage());
    }

    /**
     * 数据操作异常
     *
     * @param e 异常对象
     * @return ResponseResult
     */
    @ExceptionHandler(value = DataBaseException.class)
    public ApiResult handle(DataBaseException e) {
        log.error(e.getMessage());
        return ApiResult.fail().setMessage("database: " + e.getMessage());
    }

    /**
     * 无效的参数异常
     *
     * @param e 异常对象
     * @return ResponseResult
     */
    @ExceptionHandler(value = ParamException.class)
    public ApiResult handle(ParamException e) {
        log.error(e.getMessage());
        return ApiResult.fail().setMessage("invalid parameter: " + e.getMessage());
    }
}
